package com.mxprojects.io
{
	import com.mxprojects.constructors.InputConstructor;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.ContextMenuEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	public class Utils
	{
		public static function escapeRegEx(stringToEscape:String):String {
			return stringToEscape.replace(new RegExp("([{}\(\)\^$&.\*\?\/\+\|\[\\\\]|\]|\-)","g"), "\\$1");
		}
		
		public static function buildBlankString(spaceCount:int):String {
			var blankString:String = "";
			
			for(var i:int = 0; i < spaceCount; i++){
				blankString += " ";
			}
			
			return blankString;
		}
		
		public static function getLargestArrayElement(array:Array):int {
			var largest:int = -1;
			var length:int = 0;
			
			//loop through the array and get the length of the element
			for(var i:int = 0; i < array.length; i++){
				if(array[i].length > length){
					largest = i;
					length = array[i].length;
				}
			}
			return largest;
		}
		
		public static function getLargestInputConstructor(holder:DisplayObjectContainer):InputConstructor {
			//this will return the largest input based on it's width. Helpful for laying out pages with inputs
			var totalChildren:int = holder.numChildren;
			var largeInput:InputConstructor;
			
			for(var i:int = 0; i < totalChildren; i++){
				var child:DisplayObject = holder.getChildAt(i);
				if(child is InputConstructor){
					//if the input hasn't been set yet
					if(largeInput == null){
						largeInput = child as InputConstructor;
					}else if(child.width > largeInput.width){
						largeInput = child as InputConstructor;
					}
				}
			}
			
			return largeInput;
		}
		
		public static function hexToRGB(hex:uint):Array {
			//this will take a hex number and spit it out into RGB values
			var rgbArray:Array = new Array();
			var hexString:String = hex.toString(16);
			
			//chop up the bad boy into 2s and parse the pair as an INT
			var red:int = parseInt(hexString.substring(0,2), 16);
			var green:int = parseInt(hexString.substring(2,4), 16);
			var blue:int = parseInt(hexString.substring(4,6), 16);
			
			//push the elements into the array
			rgbArray.push(red, green, blue);
			
			return rgbArray;
		}
		
		public static function numberFormat(numberWithoutCommas:Number):String {
			//http://snipplr.com/view.php?codeview&id=7740 modified slightly
			var pattern:RegExp = /(\d+)(\d{3}(\.|,|$))/gi;
			var str:String = String(numberWithoutCommas);
			
			while (str.match(pattern).length != 0){
				str = (str.replace(pattern, "$1,$2"));	
			}
			
			return str;
		}
		
		public static function createRightClickMenu():void {
			//tossing this in here to put in a linkback to MXP
			var menu:ContextMenu = new ContextMenu();
			var author:ContextMenuItem = new ContextMenuItem("MXP3 by Scott Ysebert");
			
			//head to the website to get more info, updates, etc.
			author.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, visitSite);
			
			//hide the built in menu items
			menu.hideBuiltInItems();
			
			//put in the new item(s)
			menu.customItems.push(author);
			
			//use the header... cause you can
			MXP3.header.parent.contextMenu = menu;
		}
		
		private static function visitSite(event:ContextMenuEvent):void {
			//off to the site!
			navigateToURL(new URLRequest("http://www.mxprojects.com"), "_blank");
		}

	}
}